package com.edge.pbx.view.home;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

import com.edge.pbxdsp.R;


/**
 * Copyright (C), 深圳市东乘电子科技有限公司
 * Carate By YangZhong
 */
public class PbxMainLRButtonLayout extends FrameLayout implements View.OnClickListener {
    private final int FIX_WIDTH = 3560;

    private int mMaxClick = 1;
    private float mScale = 1f;

    public PbxMainLRButtonLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int width = MeasureSpec.getSize(widthMeasureSpec);
        mScale = (float) width/FIX_WIDTH;
        final int w = (int) (282 * mScale);
        mMaxClick = w * w;
        View view = null;
        for(int i = 0, count = getChildCount(); i < count; i++){
            view = getChildAt(i);
            if (view == null)continue;
            view.measure(MeasureSpec.makeMeasureSpec(w, MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec(w, MeasureSpec.EXACTLY));
        }
        setMeasuredDimension(width, w);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        for(int i = 0, count = getChildCount(); i < count; i++){
            getChildAt(i).setOnClickListener(this);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int width = r - l;
        View view = getChildAt(0);
        if (view != null){
            view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        }
        view = getChildAt(1);
        if (view != null){
            view.layout(width-view.getMeasuredWidth(), 0, width, view.getMeasuredHeight());
        }

    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.pbx_main_show_left_menu:
                getPbxMainMenuRootLayout().showHideLeftMenu(true);
                break;
            case R.id.pbx_main_show_right_menu:
                getPbxMainMenuRootLayout().showHideRightMenu(true);
                break;
        }
    }
    private PbxMainMenuRootLayout mPbxMainMenuRootLayout;
    private PbxMainMenuRootLayout getPbxMainMenuRootLayout(){
        if (mPbxMainMenuRootLayout == null){
            mPbxMainMenuRootLayout = ((Activity)getContext()).findViewById(R.id.pbx_main_menu_root_layout);
        }
        return mPbxMainMenuRootLayout;
    }

   /* @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if(ev.getAction() == MotionEvent.ACTION_DOWN){
            float x= ev.getX();
            if ( (x*x + ev.getY() * ev.getY()) < mMaxClick){
                return super.dispatchTouchEvent(ev);
            }
            x = ev.getX() - getWidth();
            if ( (x*x + ev.getY() * ev.getY()) < mMaxClick){
                return super.dispatchTouchEvent(ev);
            }
            return true;
        }
        return super.dispatchTouchEvent(ev);
    }*/
}
